package demo2;

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public static void main(String[] args) {
        // 数字转字符串
        String s1 = String.valueOf(1234);
        String s2 = String.valueOf(12.34);
        String s3 = String.valueOf(true);
        String s4 = String.valueOf(new Student("frost cold", 18));

        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);

        System.out.println("=================================");

        // 字符串转数字
        int data1 = Integer.parseInt("1234");
        double data2 = Double.parseDouble("12.34");
        System.out.println(data1+1);
        System.out.println(data2+1);
    }
}

public class Test {

    public static void main(String[] args) {
        // String变为StringBuilder：利用StringBuilder的构造方法
        String s = "frost";
        StringBuilder stringBuilder = new StringBuilder(s);

        // StringBuilder变为String：调用toString()方法
        StringBuilder sb = new StringBuilder("cold");
        String str = sb.toString();
    }

    public static void main21(String[] args) {
        String str = "frost";
        str += "cold";
        System.out.println(str);
    }

    public static void main20(String[] args) {
        char[] ch = new char[]{'a', 'b', 'c'};
        String s1 = new String(ch); // s1对象并不在常量池中
        s1.intern(); // s1.intern()调用之后，会将s1对象的引用放入到常量池中
        String s2 = "abc"; // "abc" 在常量池中存在了，s2创建时直接用常量池中"abc"的引用
        System.out.println(s1 == s2); // true
    }

    public static void main19(String[] args) {
        String s1 = "frost";
        String s2 = "frost";
        String s3 = new String("cold");
        String s4 = new String("cold");

        System.out.println(s1 == s2); // true
        System.out.println(s1 == s3); // false
        System.out.println(s3 == s4); // false
    }

    public static void main18(String[] args) {
        String s1 = "hello";
        String s2 = "hello";
        System.out.println(s1 == s2); // true
    }

    public static void main17(String[] args) {
        String str1 = "frost";
        String str2 = "frost";
        String str3 = new String("frost");
        String str4 = new String("frost");

        System.out.println(str1 == str2); // true
        System.out.println(str1 == str3); // false
        System.out.println(str3 == str4); // false
    }

    public static void main16(String[] args) {
        String str = "   frost cold   ";
        System.out.println(str);
        System.out.println(str.trim());
    }

    public static void main15(String[] args) {
        String str = "frost cold";
        String str2 = str.substring(2, 7);
        System.out.println(str2);
    }

    public static void main14(String[] args) {
        String str = "name=frostcold&age=18";
        String[] str2 = str.split("=|&");
        for (String e : str2) {
            System.out.println(e);
        }
    }

    public static void main13(String[] args) {
        String str = "frost cold frost cold";
        String[] str2 = str.split(" ", 3);
        for (String e : str2) {
            System.out.println(e);
        }
    }

    public static void main12(String[] args) {
        String str = "192.168.1.1";
        String[] str2 = str.split("\\.");
        for (String e : str2) {
            System.out.println(e);
        }
    }

    public static void main11(String[] args) {
        String str = "frost cold";
        String[] str2 = str.split(" ");
        for (String e : str2) {
            System.out.println(e);
        }
    }

    public static void main10(String[] args) {
        String str = "abcdabcdababbbbbbab";
        String strReplace = str.replace("ab", "xy");
        String strReplace2 = str.replaceAll("ab", "xy");
        System.out.println(strReplace);
        System.out.println(strReplace2);

        String strReplace3 = str.replaceFirst("ab", "xy");
        System.out.println(strReplace3);
    }

    public static void main9(String[] args) {
        String s = String.format("%d-%d-%d", 2024, 3, 10);
        System.out.println(s); // 2024-3-10

        String s2 = String.format("%d.%d.%d", 2024, 2, 27);
        System.out.println(s2); // 2024.2.27

        String s3 = String.format("%d年%d月%d日", 2024, 3, 10);
        System.out.println(s3); // 2024年3月10日
    }

    public static void main8(String[] args) {
        String str = "frost cold";
        char[] ch = str.toCharArray();
        for (char c : ch) {
            System.out.print(c);
        } // frost cold
        System.out.println();
        // 数组转字符串
        String str2 = new String(ch);
        System.out.println(str2); // frost cold
    }

    public static void main7(String[] args) {
        String str1 = "frost cold";
        String str2 = "FROST COLD";

        // 小写转大写
        System.out.println(str1.toUpperCase()); // FROST COLD
        // 大写转小写
        System.out.println(str2.toLowerCase()); // frost cold
    }

    public static void main6(String[] args) {
        String str = "this is a potential bloggers";
        int ch = str.lastIndexOf("is", 8);
        int ch2 = str.lastIndexOf("is", 4);
        System.out.println(ch); // 从下标8开始找，从后往前找输出第一次出现is的位置5
        System.out.println(ch2); // 从下标4开始找，从后往前找输出第一次出现is的位置2
    }

    public static void main5(String[] args) {
        String str = "this is a potential bloggers";
        int ch = str.lastIndexOf("is");
        int ch2 = str.lastIndexOf("ab");
        System.out.println(ch); // is在下标为2和5的地方都出现了，从后往前找输出第一次出现的位置5
        System.out.println(ch2); // str中没有ab,输出-1
    }

    public static void main4(String[] args) {
        String str = "frost cold";
        int ch = str.lastIndexOf('o', 5);
        int ch2 = str.lastIndexOf('o', 1);
        System.out.println(ch); // 输出从下标6开始第一次出现的位置2
        System.out.println(ch2); // 从下标1开始没有o,输出-1
    }

    public static void main3(String[] args) {
        String str = "frost cold";
        char ch = str.charAt(3);
        char ch2 = str.charAt(10);
        System.out.println(ch); // 输出下标为3的字符s
        System.out.println(ch2); // StringIndexOutOfBoundsException
    }

    public static void main2(String[] args) {
        String s1 = "abc";
        String s2 = "ad";
        String s3 = "abc";
        String s4 = "abcdefg";

        System.out.println(s1.compareTo(s2)); // 不同返回字符差值-2
        System.out.println(s1.compareTo(s3)); // 相同返回0
        System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,返回长度差值-4
    }

    public static void main1(String[] args) {
        String s1 = new String("abc");
        String s2 = new String("ac");
        String s3 = new String("ABc");
        String s4 = new String("aBcdefg");

        System.out.println(s1.compareToIgnoreCase(s2)); // 不同输出字符差值-1
        System.out.println(s1.compareToIgnoreCase(s3)); // 相同输出 0
        System.out.println(s1.compareToIgnoreCase(s4)); // 前k个字符完全相同，输出长度差值-4
    }
}
